package gowcage.tfgame;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

public class Gun {//武器
	float x, y;//武器坐标
	float nx, ny;//目标对象坐标
	int firetime;//开火间隔时间
	long along;//计时器
	boolean fire;//是否开火
	float fireLong=70;//攻击距离
	Gun(float x,float y) {
		this.x = x;
		this.y = y;
		firetime = 8;
		fire = false;
	}
	void fire(Npc n[]) {
		along++;
		if (along % firetime == 0) {
			float c[] = new float[n.length];
			for (int i = 0; i < n.length; i++) {
				if (n[i].hp > 0) {
					float a = Math.abs(n[i].x - x);
					float b = Math.abs(n[i].y - y);
					c[i] = a * a + b * b;
					if (Math.sqrt(c[i]) < fireLong) {
						nx = n[i].x;
						ny = n[i].y;
						n[i].hp -= 1;
						Log.v("fff=", i + "+" + nx + "," + ny);
						fire = true;
						break;
					}
				}
			}

		}
	}

	void paint(Canvas c, Paint p) {
		p.setColor(Color.GREEN);
		c.drawRect(x - 15, y - 15, x + 15, y + 15, p);
		if (fire) {
			c.drawLine(x, y, nx, ny, p);
			fire = false;
		}
	}
}
